#include "system_stm32f0xx.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "iwdg.h"
#include "usart.h"
#include <stdio.h>
#include <string.h>

void ledProc(uint32_t currentTick) {
    static uint32_t oldTick = 0;
    if(currentTick - oldTick >= 1000) { // 每1s
        togglePin(GPIOA, 4); // 翻转一次LED引脚
        IWDG_FEED; // 喂狗
        oldTick = currentTick;
    }
}

void uartProc(uint32_t currentTick) {
    static uint32_t oldTick = 0;
    if(currentTick - oldTick > 1000) {
        if(g_usart_rx_sta) {
            /* 串口接收数据开始 */
            printf("%s\n",g_usart_rx_buf);

            /* 串口接收数据结束 */
            memset(g_usart_rx_buf,0,(g_usart_rx_sta+1));
            g_usart_rx_sta = 0;
        }
        oldTick = currentTick;
    }
}

int main(void) {
  SystemCoreClockUpdate(); // 更新SystemCoreClock
  ledInit();
  usart_init(48, 115200);
  wkupInit();
  SysTick_Config(SystemCoreClock/1000); // 设置SysTick中断时间间隔1ms
  iwdgInit(0x06, 3125); // 20秒
  __enable_irq();
  for(;;) {
    ledProc(currentTick);
    uartProc(currentTick);
  }
  return 0;
}